package com.whenjun.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Auther: whenjun
 * @Date: 2023/6/5 0005 17:00
 * @Description:
 */
@Component
public class InterceptorConfig implements WebMvcConfigurer {

    @Autowired
    private ApiInterceptor apiInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] addPathPatterns = {    //拦截所有请求
                "/**"
        };
        String[] excludePathPatterns = {    //设置不想拦截的路径
                "/doc.html",
                "/swagger**",
                "/v2/api-docs"
        };
        registry.addInterceptor(apiInterceptor)
        .addPathPatterns(addPathPatterns)
        .excludePathPatterns(excludePathPatterns);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        /**
         * 所有请求都允许跨域，使用这种配置就不需要 在interceptor中配置header了
         */
        registry.addMapping("/**").allowCredentials(true).allowedOriginPatterns("*")
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE").allowedHeaders("*").maxAge(3600);
    }

}
